<template>
	<view class="container">
		<view class="firstView">
			<text class="topTxt">{{orderItem.name}}</text>
		</view>
		<view class="list-box">
			<view class="list-item">
				<text class="staticDes">考场名称2</text>
				<text class="fw6">{{orderItem.ground_name}}</text>
			</view>
			<view class="list-item">
				<text class="staticDes">路线名称</text>
				<text class="fw6">{{orderItem.line_name}}</text>
			</view>
			<view class="list-item">
				<text class="staticDes">优惠金额</text>
				<text class="fw6">0 元</text>
			</view>
			<view class="list-item">
				<text class="staticDes">待付金额</text>
				<text class="fw6">{{(orderItem.price/100).toFixed(2)}}元</text>
			</view>
			<view class="list-item">
				<text class="staticDes">支付方式</text>
			</view>
		</view>
		<view class="flex-box" @click="choosePayWay('wechatpay')">
			<view>
				<image src="/static/icon_wechat.png"></image>微信
			</view>
			<uni-icons v-if="payType == 'wechatpay'" type="checkbox" size="26" color="#00aaff"></uni-icons>
			<uni-icons v-else type="circle" size="26" color="#b5b5b5"></uni-icons>
		</view>
		<view class="btn-box">
			<button type="primary" :disabled="clickDisabled" @click="reqcreateOrder()">提交订单</button>
		</view>

		<!-- <br />
		openid：{{openid}}
		<br />
		获取订单号：{{Orderres}}
		<br />
		拉起支付参数：{{payment_detailres}}
		<br />
		<view class="btn-box">
			appId:<input placeholder="请输入:appid" border="surround" v-model="paymentParams.appid"></input>
			timeStamp:<input placeholder="请输入:timeStamp" border="surround" v-model="paymentParams.timeStamp"></input>
			nonceStr:<input placeholder="请输入:nonceStr" border="surround" v-model="paymentParams.nonceStr"></input>
			package:<input placeholder="请输入:package" border="surround" v-model="paymentParams.package"></input>
			signType:<input placeholder="请输入:signType" border="surround" v-model="paymentParams.signType"></input>
			paySign:<input placeholder="请输入:paySign" border="surround" v-model="paymentParams.paySign"></input>
			
			WeixinJSBridge.invoke:
		</view> -->
		<!-- <button type="primary" @click="submitPay2()">确认支付</button> -->
		
		
		
		<u-tabbar v-model="current" :height="100" :list="list"></u-tabbar>
	</view>
</template>

<script>
	var jweixin = require('@/common/js_sdk/jweixin-module@1.6.0.js');
	var _this=null
	import {
		createOrder,
		postpayment_detail
	} from '@/common/api/index.js'
	import {
		goPaying,
	} from '@/utils/index.js'

	export default {
		data() {
			return {
				orderItem: {},
				payType: 'wechatpay',
				commodity_id: 0, //商品ID
				ground_id: 0, //考场ID
				line_id: 0, //路线ID
				user_id: 0, //用户ID
				count: 1, //商品数量，目前都是设置1
				device_id: 1, //设备ID，当是扫码过来时才有值
				instructor_id: 0, //教练ID，当是扫码过来时才有值
				instructor_name: "", //教练名字，当是扫码过来时才有值

				clickDisabled: false, //是否可以支付
				Orderres: {},
				orderNo: null,
				payment_detailres: null,


				openid: null,
				paymentParams:{
					appid:null,
					timeStamp:null,
					nonceStr:null,
					package:null,
					signType:null,
					paySign:null,
				},
				imei1:null,
				androidID:null,
				serial:null,
				uuid:null,
				mac:null,
				deviceUnique:null,
				clazz:null,
				channel:null,
				
				
				
				list: [{
						pagePath: "/pages/student/index/index",
						iconPath: "/static/tabbar/tab_home_normal.png",
						selectedIconPath: "/static/tabbar/tab_home_select.png",
						text: '首页',
					},
					{
						pagePath: "/pages/student/order/index",
						iconPath: "/static/tabbar/tab_order_normal.png",
						selectedIconPath: "/static/tabbar/tab_order_select.png",
						text: '订单',
					},
					{
						pagePath: "/pages/student/my/index",
						iconPath: "/static/tabbar/tab_mine_normal.png",
						selectedIconPath: "/static/tabbar/tab_mine_select.png",
						text: '我的',
					},
				],
				current: 0,
				Coach_id:0,
				commodity_id_o:0,
			}
		},

		onLoad(data) {
			this.orderItem = JSON.parse(data.orderDetail)
			this.commodity_id = this.orderItem.id;
			this.ground_id = this.orderItem.ground_id;
			this.line_id = this.orderItem.line_id;
			this.user_id = uni.getStorageSync('userid');
			
			this.imei1= uni.getStorageSync('imei1');
			this.androidID= uni.getStorageSync('androidID');
			this.serial= uni.getStorageSync('serial');
			this.uuid= uni.getStorageSync('uuid');
			this.mac= uni.getStorageSync('mac');
			this.deviceUnique= uni.getStorageSync('deviceUnique');
			this.clazz= uni.getStorageSync('clazz');
			this.channel= uni.getStorageSync('channel'); 
			
			
			
			
			let Coach_id_o = uni.getStorageSync('Coach_ID');
			let commodity_id_o = uni.getStorageSync('commodity_id');
			if(Coach_id_o){
				this.Coach_id=Coach_id_o
			}
			if(commodity_id_o){
				this.commodity_id_o=commodity_id_o
			}
			
			console.log("接收到的参数", this.orderItem);
			console.log("userid: ", this.user_id);

			this.openid = uni.getStorageSync('openId')
			_this=this
		},

		components: {

		},
		methods: {
			reqcreateOrder() {
				var params = {
					commodity_id: this.commodity_id, //商品ID
					ground_id: this.ground_id, //考场ID
					line_id: this.line_id, //路线ID
					user_id: this.user_id, //用户ID	
					count: 1, //商品数量，目前都是设置1
					device_id: 1, //设备ID，当是扫码过来时才有值
					instructor_id: 1, //教练ID，当是扫码过来时才有值
					instructor_name: "测试王", //教练名字，当是扫码过来时才有值
				}
				
				let studentQuery = uni.getStorageSync('studentQuery');
				if(studentQuery!=null&&studentQuery!=""){
					console.log(studentQuery);
					let parsestudentQuery= JSON.parse(studentQuery)
					if(parsestudentQuery){
						for (let key in parsestudentQuery) {
							params[key]=parsestudentQuery[key]
						}
					}
				}
				console.log(params,'拉起支付-params');
				
				createOrder(params).then(res => {
					this.Orderres = res
					console.log(res, '有结果吗')
					if (res.status == 0) {
						this.orderNo = res.data.orderNo
						this.submitPay()
					}
					// if (res.status == 0) {
					// 	this.manageRes(res.trade_no)
					// } else if (res.status == 97978) {
					// 	// this.manageRes(res.trade_no)
					// 	showTishi(res.detail)
					// } else {
					// 	this.showPay = false
					// 	var types = res.status == 30008 ? 'time_remaining' : 'time_expenses'
					// 	if (res.trade_no) {
					// 		res.trade_no.plateColor = formatPlateColor(res.trade_no.plate_color)
					// 		res.trade_no.chargingTime = getDataTime(res.trade_no.charged_duration)
					// 		this.orderDatail = res.trade_no;
					// 		if (this.orderDatail.timespan) {
					// 			this.countDown(this.orderDatail.timespan, types)
					// 		} else {
					// 			this.$kdic.billOrderStatus(res.status)
					// 		}
					// 	}
					// }
				})
			},
			// 拉起微信支付
			submitPay() {
				// this.reqcreateOrder();
				let params = {
					orderNo: this.orderNo,
					amount: 1,
					netreceipts: 1,
					channel:this.channel,
					openid:this.openid,
					// discount:0,
					// coupon_fee:0,
				}
				if(Number(this.Coach_id)!=0){
					console.log(this.commodity_id_o,this.commodity_id,this.commodity_id_o==this.commodity_id,'this.commodity_id_o');
					if(this.commodity_id_o==this.commodity_id){
						params.instructor_id=Number(this.Coach_id)
					}
				}
				postpayment_detail(params).then(res => {
					console.log(res);
					this.payment_detailres = res
					
					this.paymentParams.appid=res.data.appid
					this.paymentParams.timeStamp=res.data.timestamp
					this.paymentParams.nonceStr=res.data.noncestr
					this.paymentParams.package=res.data.package
					this.paymentParams.signType="RSA"
					this.paymentParams.paySign=res.data.sign 
					this.submitPay2()
				})
				// 假设你已经获取了统一下单接口返回的 prepay_id
				var prepay_id = 'xxxxx'; // 假设这是从后端获取的 prepay_id
			},
			submitPay3(){
				// wx.requestPayment({
				// wx.requestPayment({
				WeixinJSBridge.invoke({
				  appId: _this.paymentParams.appid,
				  timeStamp: _this.paymentParams.timeStamp,
				  nonceStr: _this.paymentParams.nonceStr,
				  package: _this.paymentParams.package,
				  signType: _this.paymentParams.signType,
				  paySign:_this.paymentParams.paySign,
				  success(res) {
				    // 支付成功的回调
				    console.log('支付成功', res);
				  },
				  fail(err) {
				    // 支付失败的回调
				    console.error('支付失败', err);
				  }
				});
			},
			submitPay2(){
				
				
				
				
				// console.log('跳转页面');
				// uni.redirectTo({
				// 	url: '/pages/student/index/index',
				// 	success: function(res) {
				// 		// 跳转成功的回调
				// 		console.log(res, '跳转成功')
				// 	},
				// 	fail: function(err) {
				// 		// 错误处理
				// 		console.log(err, '为什么不跳转')
				// 	}
				// })
				jweixin.config({
					openid:_this.openid,
					appId: _this.paymentParams.appid, // 必填，公众号的唯一标识
					timestamp: _this.paymentParams.timeStamp, // 必填，生成签名的时间戳
					nonceStr: _this.paymentParams.nonceStr, // 必填，生成签名的随机串
					signature: _this.paymentParams.paySign, // 必填，签名，见附录1
					jsApiList: ['chooseWXPay'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
				});
				jweixin.ready(function() {
					// config信息验证后会执行ready方法，所有接口调用都必须在config接口获得结果之后，config是一个客户端的异步操作，所以如果需要在页面加载时就调用相关接口，则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口，则可以直接调用，不需要放在ready函数中。
					jweixin.chooseWXPay({
						openid:_this.openid,
						appId: _this.paymentParams.appId, //公众号名称，由商户传入
						timestamp: _this.paymentParams.timeStamp, //时间戳，自1970年以来的秒数
						nonceStr: _this.paymentParams.nonceStr, //随机串
						package: _this.paymentParams.package,
						signType: _this.paymentParams.signType, //微信签名方式：
						paySign: _this.paymentParams.paySign, //微信签名
						success: function(res) {
							uni.showToast({
								title: '支付成功！'
							})
							wx.switchTab({
								url: '/pages/student/order/index', // 替换为你的 tabBar 页面路径
								success: function(res) {
									// 跳转成功的回调
									console.log(res, '跳转成功')
								},
								fail: function(err) {
									// 错误处理
									console.log(err, '为什么不跳转')
								}
							});
						},
						cancel: function(res) {
							console.log('取消支付' + res)
							uni.showToast({
								title: '取消支付',
								icon: 'none'
							})
						},
						fail: function(res) {
							console.log('支付失败',res)
							uni.showToast({
								title: '支付失败',
								icon: 'none'
							})
						}
					})
				});
				jweixin.error(function(res) {
					console.log('身份验证失效' + res)
				});
			}
		},
	}
</script>

<style lang="scss" scoped>
	.mui-content {
		height: 100vh;
		background: #F7F9FB;
	}

	.firstView {
		display: flex;
		padding: 24rpx;
		font-size: 36rpx;
		font-weight: bold;
		justify-content: center;
		align-items: center;
	}


	.list-item {
		padding: 30rpx 30rpx 20rpx;
		border-bottom: 2rpx solid #eee;
		color: #969696;
		font-size: 28rpx;
		display: flex;
		justify-content: space-between;

		.staticDes {
			color: #333333;
			font-size: 30rpx;
		}

		.fw6 {
			color: #3a3a3a;
			font-size: 32rpx
		}
	}

	.flex-box {
		display: flex;
		justify-content: space-between;
		margin: 10px;

		image {
			width: 46rpx;
			height: 46rpx;
			padding-right: 10rpx;
			vertical-align: middle;
		}

		view {
			&:nth-child(2) {
				color: #747373;
			}
		}

		view.red-text {
			color: red;
		}
	}

	.btn-box {
		width: 92%;
		margin-left: 4%;
		padding-bottom: 10px;
		margin-top: 20px;
	}
</style>